<?php
	use PHPLog\Install_File;
	use PHPLog\Install\Common;
	
	require_once "../Common/CommonFunctions.php";

	$str_CodeInfo = Common\clean_Path(dirname(dirname(dirname(dirname(__FILE__)))) . "/Code/cCodeInfo.php");
	$str_History = Common\clean_Path(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . "/PHP_Updater/code/Classes/History/cHistory.php");
	$str_Version = Common\clean_Path(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . "/PHP_Updater/code/Classes/cVersion.php");

	require_once $str_CodeInfo;
	require_once $str_History;
	require_once $str_Version;

	//Set Global Variables
	//$GLOBALS ["PATH_CODE"] = Install_File\cCodeInfo::C_STR_CODE;
	$GLOBALS["InstallPath"] = Common\clean_Path($_POST["C_STR_CODE"] . "/Code");

	$str_History_Path = Common\clean_Path(dirname($GLOBALS["InstallPath"]) . "/Setup/Install_History.xml");

	print("<h3>Set Config Files</h3>");

	function create_Dir ()
	{
		Common\create_Directory($GLOBALS["InstallPath"]);
		Common\create_Directory(dirname($GLOBALS["InstallPath"]) . "/Setup");
		print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Created Directory - " . $GLOBALS["InstallPath"] . "<br/>");
	}

	function save_Code_Config ()
	{
		$str_Code_Path = Common\clean_Path($GLOBALS["InstallPath"] . "/cCodeInfo.php");

		$obj_Action = $GLOBALS["Install_History_File"]->create_ActionElement();
		$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_DEST, $str_Code_Path);

		if (!file_exists($str_Code_Path))
		{
			$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE_CREATE);
		}
		else
		{
			$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE_MODIFY);
		}
		$bool_Return = false;

		$str_File = "";
		$obj_File = @fopen($str_Code_Path, "w+");
		if ($obj_File)
		{
			$str_File .= "<?php\n";
			$str_File .= "namespace PHPLog;\n";
			$str_File .= "class cCodeInfo\n";
			$str_File .= "{\n";
			foreach ($_POST as $str_Key => $str_Data)
			{
				$str_File .= "const " . $str_Key . " = \"" . $str_Data . "\";\n";
			}
			$str_File .= "}\n";
			$str_File .= "?>";

			$bool_Return = fwrite($obj_File, $str_File);
			fclose($obj_File);
		}

		if ($bool_Return)
		{
			$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS_SUCCESS);
		}
		else
		{
			$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS_FAIL);
		}

		$GLOBALS["Install_History"]->appendChild(clone $obj_Action);
	}

	Common\open_History($str_History_Path);
	Common\set_Update_Site("http://do-php-logging.googlecode.com/files/Updates.xml");

	print("Create Required Directories...<br/>");
	create_Dir();
	print("&nbsp; - Successful<br/>");
	print("Create Config Files...<br/>");
	save_Code_Config();
	print("&nbsp; - Successful<br/>");

	Common\save_History();
?>